From cc1524d0a55fc52bc76ead39ae34abb0d05b78a6 Mon Sep 17 00:00:00 2001 From: robertl Date: Sun, 19 Jan 2003 22:16:12 +0000 Subject: [PATCH] introduce shortname handles in all file formats. Make -s rename duplicate generated shortnames. --- gpsbabel/csv.c | 7 +- gpsbabel/csv_util.c | 13 ++-- gpsbabel/defs.h | 15 +++-- gpsbabel/dna.c | 7 +- gpsbabel/garmin.c | 10 ++- gpsbabel/gpsdrive.c | 11 ++- gpsbabel/gpsutil.c | 7 +- gpsbabel/gpx.c | 8 ++- gpsbabel/holux.c | 8 ++- gpsbabel/magproto.c | 14 ++-- gpsbabel/mapsend.c | 6 +- gpsbabel/mapsource.c | 10 ++- gpsbabel/mkshort.c | 157 ++++++++++++++++++++++++++++++++++++------- gpsbabel/mxf.c | 10 ++- gpsbabel/ozi.c | 11 ++- gpsbabel/pcx.c | 9 ++- gpsbabel/psp.c | 10 ++- gpsbabel/tiger.c | 24 ++++--- gpsbabel/tmpro.c | 11 +-- gpsbabel/tpg.c | 9 ++- gpsbabel/waypt.c | 7 +- gpsbabel/xcsv.c | 11 +-- gpsbabel/xmapwpt.c | 9 ++- 23 files changed, 291 insertions(+), 93 deletions(-) diff --git a/gpsbabel/csv.c b/gpsbabel/csv.c index 3d6922fde..4302bb079 100644 --- a/gpsbabel/csv.c +++ b/gpsbabel/csv.c @@ -26,6 +26,7 @@ static FILE *file_in; static FILE *file_out; +static void *mkshort_handle; static char *psn; #define MYNAME "CSV" @@ -34,6 +35,8 @@ static void rd_init(const char *fname, const char *args) { file_in = fopen(fname, "r"); + mkshort_handle = mkshort_new_handle(); + if (file_in == NULL) { fatal(MYNAME ": Cannot open %s for reading\n", fname); } @@ -42,6 +45,7 @@ rd_init(const char *fname, const char *args) static void rd_deinit(void) { + mkshort_del_handle(mkshort_handle); fclose(file_in); } @@ -49,6 +53,7 @@ static void wr_init(const char *fname, const char *args) { file_out = fopen(fname, "w"); + psn = get_option(args, "prefer_shortname"); if (file_out == NULL) { @@ -126,7 +131,7 @@ data_read(void) /* We'll make up our own shortname. */ if (wpt_tmp->description) { - wpt_tmp->shortname = mkshort(wpt_tmp->description); + wpt_tmp->shortname = mkshort(mkshort_handle, wpt_tmp->description); waypt_add(wpt_tmp); } diff --git a/gpsbabel/csv_util.c b/gpsbabel/csv_util.c index cf5ee4a2f..3b5f2cecf 100644 --- a/gpsbabel/csv_util.c +++ b/gpsbabel/csv_util.c @@ -34,6 +34,8 @@ #define EXCEL_TO_TIMET(a) ((a - 25569.0) * 86400.0) #define TIMET_TO_EXCEL(a) ((a / 86400.0) + 25569.0) +static void *mkshort_handle; + /*********************************************************************/ /* csv_stringclean() - remove any unwanted characters from string. */ /* returns copy of string. */ @@ -563,10 +565,10 @@ xcsv_waypt_pr(const waypoint *wpt) queue *elem, *tmp; if (wpt->shortname) { - anyname = xstrdup(wpt->shortname); + anyname = xstrdup(mkshort(mkshort_handle, wpt->shortname)); } else if (wpt->description) { - anyname = xstrdup(wpt->description); + anyname = xstrdup(mkshort(mkshort_handle, wpt->description)); } else if (wpt->notes) { anyname = xstrdup(wpt->notes); @@ -574,13 +576,13 @@ xcsv_waypt_pr(const waypoint *wpt) anyname = xstrdup(""); if ((anyname) && (global_opts.synthesize_shortnames)) { - anyname = mkshort(anyname); + anyname = mkshort(mkshort_handle, anyname); } if ((! wpt->shortname) || (global_opts.synthesize_shortnames)) { if (wpt->description) { if (global_opts.synthesize_shortnames) - shortname = mkshort(wpt->description); + shortname = mkshort(mkshort_handle, wpt->description); else shortname = csv_stringclean(wpt->description, xcsv_file.badchars); } else { @@ -764,13 +766,13 @@ xcsv_data_write(void) { queue *elem, *tmp; ogue_t *ogp; + mkshort_handle = mkshort_new_handle(); /* output prologue lines, if any. */ QUEUE_FOR_EACH(&xcsv_file.prologue, elem, tmp) { ogp = (ogue_t *) elem; fprintf (xcsv_file.xcsvfp, "%s%s", ogp->val, xcsv_file.record_delimiter); } - waypt_disp_all(xcsv_waypt_pr); /* output epilogue lines, if any. */ @@ -778,5 +780,6 @@ xcsv_data_write(void) ogp = (ogue_t *) elem; fprintf (xcsv_file.xcsvfp, "%s%s", ogp->val, xcsv_file.record_delimiter); } + mkshort_del_handle(mkshort_handle); } diff --git a/gpsbabel/defs.h b/gpsbabel/defs.h index d7c6f3186..60a4aeb7f 100644 --- a/gpsbabel/defs.h +++ b/gpsbabel/defs.h @@ -180,11 +180,16 @@ void route_add (waypoint *); void route_add_wpt(route_head *rte, waypoint *wpt); void route_add_head(route_head *rte); -char *mkshort (const char *); -void setshort_length(int n); -void setshort_badchars(const char *); -void setshort_mustupper(int n); -void setshort_whitespace_ok(int n); +/* + * All shortname functions take a shortname handle as the first arg. + * This is an opaque pointer. Callers must not fondle the contents of it. + */ +char *mkshort (void *, const char *); +void *mkshort_new_handle(void); +void setshort_length(void *, int n); +void setshort_badchars(void *, const char *); +void setshort_mustupper(void *, int n); +void setshort_whitespace_ok(void *, int n); typedef struct ff_vecs { ff_init rd_init; diff --git a/gpsbabel/dna.c b/gpsbabel/dna.c index 54eb791be..d10430c5e 100644 --- a/gpsbabel/dna.c +++ b/gpsbabel/dna.c @@ -28,6 +28,7 @@ static FILE *file_in; static FILE *file_out; +static void *mkshort_handle; #define MYNAME "DNA" @@ -35,6 +36,8 @@ static void rd_init(const char *fname, const char *args) { file_in = fopen(fname, "r"); + mkshort_handle = mkshort_new_handle(); + if (file_in == NULL) { fatal(MYNAME ": Cannot open %s for reading\n", fname); } @@ -43,6 +46,7 @@ rd_init(const char *fname, const char *args) static void rd_deinit(void) { + mkshort_del_handle(mkshort_handle); fclose(file_in); } @@ -50,6 +54,7 @@ static void wr_init(const char *fname, const char *args) { file_out = fopen(fname, "w"); + if (file_out == NULL) { fatal(MYNAME ": Cannot open %s for writing\n", fname); } @@ -111,7 +116,7 @@ data_read(void) wpt_tmp->creation_time = time(NULL); /* We'll make up our own shortname. */ - wpt_tmp->shortname = mkshort(wpt_tmp->description); + wpt_tmp->shortname = mkshort(mkshort_handle, wpt_tmp->description); waypt_add(wpt_tmp); diff --git a/gpsbabel/garmin.c b/gpsbabel/garmin.c index 2ef89bd3f..5fa38cb72 100644 --- a/gpsbabel/garmin.c +++ b/gpsbabel/garmin.c @@ -26,10 +26,14 @@ #define MYNAME "GARMIN" static const char *portname; +static void *mkshort_handle; static void rw_init(const char *fname, const char *opts) { + if (!mkshort_handle) + mkshort_handle = mkshort_new_handle(); + if (global_opts.debug_level > 0) { GPS_Enable_Warning(); GPS_Enable_User(); @@ -211,8 +215,8 @@ data_write(void) break; } - setshort_length(short_length); - setshort_mustupper(1); + setshort_length(mkshort_handle, short_length); + setshort_mustupper(mkshort_handle, 1); QUEUE_FOR_EACH(&waypt_head, elem, tmp) { waypoint *wpt; char *ident; @@ -238,7 +242,7 @@ data_write(void) if(wpt->notes) src = wpt->notes; ident = global_opts.synthesize_shortnames ? - mkshort(src) : + mkshort(mkshort_handle, src) : wpt->shortname; strncpy(way[i]->ident, ident, sizeof(way[i]->ident)); way[i]->ident[sizeof(way[i]->ident)-1] = 0; diff --git a/gpsbabel/gpsdrive.c b/gpsbabel/gpsdrive.c index ef9fe2b5c..3287e5d27 100644 --- a/gpsbabel/gpsdrive.c +++ b/gpsbabel/gpsdrive.c @@ -33,6 +33,8 @@ static FILE *file_in; static FILE *file_out; +static void *mkshort_wr_handle; +static void *mkshort_rd_handle; #define MYNAME "GPSDRIVE" @@ -75,6 +77,7 @@ data_read(void) int i; waypoint *wpt_tmp; int linecount = 0; + mkshort_rd_handle = mkshort_new_handle(); do { linecount++; @@ -117,7 +120,7 @@ data_read(void) /* We'll make up our own shortname. */ if (wpt_tmp->description) { - wpt_tmp->shortname = mkshort(wpt_tmp->description); + wpt_tmp->shortname = mkshort(mkshort_rd_handle, wpt_tmp->description); waypt_add(wpt_tmp); } @@ -126,6 +129,7 @@ data_read(void) } } while (!feof(file_in)); + mkshort_del_handle(mkshort_rd_handle); } static void @@ -147,7 +151,7 @@ gpsdrive_waypt_pr(const waypoint *wpt) shortname = csv_stringclean(wpt->notes, ",\""); if ( shortname ) - shortname = mkshort(shortname); + shortname = mkshort(mkshort_wr_handle, shortname); fprintf(file_out, "%s %08.5f %08.5f\n", shortname, @@ -162,7 +166,10 @@ gpsdrive_waypt_pr(const waypoint *wpt) static void data_write(void) { + mkshort_wr_handle = mkshort_new_handle(); + waypt_disp_all(gpsdrive_waypt_pr); + mkshort_del_handle(mkshort_wr_handle); } ff_vecs_t gpsdrive_vecs = { diff --git a/gpsbabel/gpsutil.c b/gpsbabel/gpsutil.c index 42fb4dbb9..4f7a86f80 100644 --- a/gpsbabel/gpsutil.c +++ b/gpsbabel/gpsutil.c @@ -7,6 +7,7 @@ static FILE *file_in; static FILE *file_out; +static void *mkshort_handle; #define MYNAME "GPSUTIL" @@ -32,12 +33,15 @@ wr_init(const char *fname, const char *args) if (file_out == NULL) { fatal(MYNAME ": Cannot open %s for writing\n", fname); } + mkshort_handle = mkshort_new_handle(); + } static void wr_deinit(void) { fclose(file_out); + mkshort_del_handle(mkshort_handle); } static void @@ -89,7 +93,8 @@ gpsutil_disp(const waypoint *wpt) fprintf(file_out, "%-8s %08.3f%c %09.3f%c %07.0f%c %-30.30s %s\n", global_opts.synthesize_shortnames ? - mkshort(wpt->description) : wpt->shortname, + mkshort(mkshort_handle, wpt->description) : + wpt->shortname, fabs(lat), lat < 0.0 ? 'S' : 'N', fabs(lon), diff --git a/gpsbabel/gpx.c b/gpsbabel/gpx.c index 98af66274..22a029273 100644 --- a/gpsbabel/gpx.c +++ b/gpsbabel/gpx.c @@ -51,6 +51,7 @@ static const char *gpx_creator; static waypoint *wpt_tmp; static FILE *fd; static FILE *ofd; +static void *mkshort_handle; #define MYNAME "GPX" #define MY_CBUF 4096 @@ -155,7 +156,6 @@ start_something_else(const char *el, const char **attrv) const char **avp = attrv; char **avcp = NULL; int attr_count = 0; - xml_tag *new_tag; if ( !wpt_tmp ) { @@ -532,6 +532,7 @@ gpx_rd_deinit(void) void gpx_wr_init(const char *fname, const char *args) { + mkshort_handle = mkshort_new_handle(); ofd = fopen(fname, "w"); if (ofd == NULL) { fatal(MYNAME ": open %s for writing\n", fname ); @@ -542,6 +543,7 @@ static void gpx_wr_deinit(void) { fclose(ofd); + mkshort_del_handle(mkshort_handle); } void @@ -632,7 +634,7 @@ gpx_waypt_pr(const waypoint *waypointp) { char *tmp_ent; const char *oname = global_opts.synthesize_shortnames ? - mkshort(waypointp->description) : + mkshort(mkshort_handle, waypointp->description) : waypointp->shortname; fprintf(ofd, "\n", @@ -724,7 +726,7 @@ void gpx_track_pr() void gpx_write(void) { - setshort_length(32); + setshort_length(mkshort_handle, 32); fprintf(ofd, "\n"); fprintf(ofd, "notes ? waypointp->notes : waypointp->description; owpt = global_opts.synthesize_shortnames ? - mkshort(isrc) : waypointp->shortname, + mkshort(mkshort_handle, isrc) : waypointp->shortname, odesc = isrc ? isrc : ""; owpt = mag_cleanse(owpt); odesc = mag_cleanse(odesc); @@ -1098,7 +1100,7 @@ mag_write(void) * Whitespace is actually legal, but since waypoint name length is * only 8 bytes, we'll conserve them. */ - setshort_whitespace_ok(0); + setshort_whitespace_ok(mkshort_handle, 0); waypt_disp_all(mag_waypt_pr); } diff --git a/gpsbabel/mapsend.c b/gpsbabel/mapsend.c index 3fdbe8224..5644133be 100644 --- a/gpsbabel/mapsend.c +++ b/gpsbabel/mapsend.c @@ -26,6 +26,7 @@ static FILE *mapsend_file_in; static FILE *mapsend_file_out; +static void *mkshort_handle; static int endianness_tested; static int i_am_little_endian; @@ -171,12 +172,14 @@ mapsend_wr_init(const char *fname, const char *args) fprintf(stderr, "Cannot open '%s' for writing\n", fname); exit(1); } + mkshort_handle = mkshort_new_handle(); } static void mapsend_wr_deinit(void) { fclose(mapsend_file_out); + mkshort_del_handle(mkshort_handle); } static void @@ -313,7 +316,8 @@ mapsend_waypt_pr(const waypoint *waypointp) double flat; static int cnt = 0; const char *sn = global_opts.synthesize_shortnames ? - mkshort(waypointp->description) : waypointp->shortname; + mkshort(mkshort_handle, waypointp->description) : + waypointp->shortname; c = strlen(sn); fwrite(&c, 1, 1, mapsend_file_out); diff --git a/gpsbabel/mapsource.c b/gpsbabel/mapsource.c index 1458c387a..d775efcfc 100644 --- a/gpsbabel/mapsource.c +++ b/gpsbabel/mapsource.c @@ -27,6 +27,7 @@ static FILE *mps_file_in; static FILE *mps_file_out; +static void *mkshort_handle; #define MYNAME "MAPSOURCE" @@ -226,7 +227,7 @@ mps_waypt_pr(const waypoint *wpt) if(wpt->description) src = wpt->description; if(wpt->notes) src = wpt->notes; ident = global_opts.synthesize_shortnames ? - mkshort(src) : + mkshort(mkshort_handle, src) : wpt->shortname; reclen = 87 + strlen(ident) + strlen(wpt->description); @@ -264,13 +265,16 @@ void mps_write(void) { int short_length = 10; + mkshort_handle = mkshort_new_handle(); - setshort_length(short_length); - setshort_whitespace_ok(0); + setshort_length(mkshort_handle, short_length); + setshort_whitespace_ok(mkshort_handle, 0); fwrite(mps_hdr, sizeof(mps_hdr), 1, mps_file_out); waypt_disp_all(mps_waypt_pr); fwrite(mps_ftr, sizeof(mps_ftr), 1, mps_file_out); + mkshort_del_handle(mkshort_handle); + } ff_vecs_t mps_vecs = { diff --git a/gpsbabel/mkshort.c b/gpsbabel/mkshort.c index e586af236..dc77471cf 100644 --- a/gpsbabel/mkshort.c +++ b/gpsbabel/mkshort.c @@ -7,15 +7,111 @@ static const char vowels[] = "aeiouAEIOU"; #define DEFAULT_TARGET_LEN 8 -static unsigned int target_len = DEFAULT_TARGET_LEN; - #define DEFAULT_BADCHARS "\"$.,'!-" -static const char *badchars = DEFAULT_BADCHARS; -static int mustupper = 0; -static int whitespaceok = 1; -static const char needmem[] = - "mkshort: could not reallocate memory for string\n"; +/* + * Hash table tunings. The reality is that our hash doesn't have to be + * terribly complex; our strings are short (typically 8-20 bytes) and the + * string hash mixes things up enough that strcmp can generally bail on the + * first byte or two for a mismatch. + */ +#define PRIME 37 + +typedef struct { + int mustupper; + int whitespaceok; + unsigned int target_len; + char *badchars; + int must_uniq; + queue namelist[PRIME]; + int depth[PRIME]; +} mkshort_handle; + +typedef struct { + queue list; + char *orig_shortname; + int conflictctr; +} uniq_shortname; + +unsigned int hash_string(const char *key) +{ + unsigned int hash = 0; + while (*key) { + hash = ((hash<<5) ^ (hash>>27)) ^ *key++; + } + hash = hash % PRIME; + return hash; +} + +void * +mkshort_new_handle() +{ + int i; + mkshort_handle *h = xcalloc(sizeof *h, 1); + + for (i = 0; i < PRIME; i++) + QUEUE_INIT(&h->namelist[i]); + + h->whitespaceok = 1; + h->badchars = DEFAULT_BADCHARS; + h->target_len = DEFAULT_TARGET_LEN; + h->must_uniq=1; + + return h; +} + +char * +mkshort_add_to_list(mkshort_handle *h, char *name) +{ + queue *e, *t; + int hash; + uniq_shortname *s = xmalloc(sizeof (uniq_shortname)); + s->orig_shortname = strdup(name); + hash = hash_string(name); + + QUEUE_FOR_EACH(&h->namelist[hash], e, t) { + uniq_shortname *z = (uniq_shortname *) e; + + if (0 == strcmp(z->orig_shortname, name)) { + int l = strlen(name); + int dl; + char tbuf[10]; + + z->conflictctr++; + dl = sprintf(tbuf, ".%d", z->conflictctr); + strcpy(&name[l-dl], tbuf); + break; + } + } + ENQUEUE_TAIL(&h->namelist[hash], &s->list); + h->depth[hash]++; + return name; +} + +void * +mkshort_is_unique() +{ +} + +void * +mkshort_del_handle(void *h) +{ + mkshort_handle *hdr = h; + int i; + + if (hdr) { + for (i = 0; i < PRIME; i++) { + queue *e, *t, *z; + QUEUE_FOR_EACH(&hdr->namelist[i], e, t) { + uniq_shortname *s = e; + dequeue(e); + free(s->orig_shortname); + free(s); + } + } + free(hdr); + } +} /* * This is the stuff that makes me ashamed to be a C programmer... @@ -50,19 +146,21 @@ delete_last_vowel(int start, char *istring, int *replaced) * strings returned by mkshort(). 0 resets to default. */ void -setshort_length(int l) +setshort_length(void *h, int l) { + mkshort_handle *hdl = h; if (l == 0) { - target_len = DEFAULT_TARGET_LEN; + hdl->target_len = DEFAULT_TARGET_LEN; } else { - target_len = l; + hdl->target_len = l; } } void -setshort_whitespace_ok(int l) +setshort_whitespace_ok(void *h, int l) { - whitespaceok = l; + mkshort_handle *hdl = h; + hdl->whitespaceok = l; } /* @@ -71,23 +169,25 @@ setshort_whitespace_ok(int l) * resets to default. */ void -setshort_badchars(const char *s) +setshort_badchars(void *h, const char *s) { + mkshort_handle *hdl = h; if (s == NULL) { - badchars = DEFAULT_BADCHARS; + hdl->badchars = DEFAULT_BADCHARS; } else { - badchars = xstrdup(s); + hdl->badchars = xstrdup(s); } } void -setshort_mustupper(int i) +setshort_mustupper(void *h, int i) { - mustupper = i; + mkshort_handle *hdl = h; + hdl->mustupper = i; } char * -mkshort(const char *istring) +mkshort(void *h, const char *istring) { char *ostring = xstrdup(istring); char *nstring; @@ -95,11 +195,12 @@ mkshort(const char *istring) char *cp; char *np; int i, l, nlen, replaced; + mkshort_handle *hdl = h; /* * Whack leading "[Tt]he", */ - if (( strlen(ostring) > target_len + 4) && + if (( strlen(ostring) > hdl->target_len + 4) && (strncmp(ostring, "The ", 4) == 0 || strncmp(ostring, "the ", 4) == 0)) { nstring = xstrdup(ostring + 4); @@ -123,7 +224,7 @@ mkshort(const char *istring) free(ostring); ostring = nstring; - if (!whitespaceok) { + if (!hdl->whitespaceok) { /* * Eliminate Whitespace */ @@ -132,7 +233,7 @@ mkshort(const char *istring) cp = ostring; for (i=0;imustupper) { tstring[i] = toupper(tstring[i]); } *cp++ = tstring[i]; @@ -150,7 +251,7 @@ mkshort(const char *istring) l = strlen (tstring); cp = ostring; for (i=0;ibadchars, tstring[i]) || !isascii(tstring[i])) continue; *cp++ = tstring[i]; } @@ -175,7 +276,7 @@ mkshort(const char *istring) * them. If we run out of string, give up. */ replaced = 1; - while (replaced && strlen(ostring) > target_len) { + while (replaced && strlen(ostring) > hdl->target_len) { ostring = delete_last_vowel(2, ostring, &replaced); } @@ -197,9 +298,15 @@ mkshort(const char *istring) * Now brutally truncate the resulting string, preserve trailing * numeric data. */ - if ((/*i = */strlen(ostring)) > target_len) { - strcpy(&ostring[target_len] - strlen(np), np); + if ((/*i = */strlen(ostring)) > hdl->target_len) { + strcpy(&ostring[hdl->target_len] - strlen(np), np); } + + + if (hdl->must_uniq) { + return mkshort_add_to_list(hdl, ostring); + } + return ostring; } diff --git a/gpsbabel/mxf.c b/gpsbabel/mxf.c index 6a90375b5..d7675b56d 100644 --- a/gpsbabel/mxf.c +++ b/gpsbabel/mxf.c @@ -33,6 +33,8 @@ #define MYNAME "MXF" +static void *mkshort_handle; + static void mxf_set_style() { @@ -65,9 +67,9 @@ mxf_set_style() /* set up mkshort */ if (global_opts.synthesize_shortnames) { - setshort_length(32); - setshort_whitespace_ok(0); - setshort_badchars(xcsv_file.badchars); + setshort_length(mkshort_handle, 32); + setshort_whitespace_ok(mkshort_handle, 0); + setshort_badchars(mkshort_handle, xcsv_file.badchars); } } @@ -86,6 +88,8 @@ mxf_rd_init(const char *fname, const char *args) static void mxf_wr_init(const char *fname, const char *args) { + mkshort_handle = mkshort_new_handle(); + mxf_set_style(); xcsv_file.xcsvfp = fopen(fname, "w"); diff --git a/gpsbabel/ozi.c b/gpsbabel/ozi.c index 9d3e8bc5b..550a99fb9 100644 --- a/gpsbabel/ozi.c +++ b/gpsbabel/ozi.c @@ -30,6 +30,9 @@ #define MYNAME "OZI" +static void *mkshort_handle; + + static void ozi_set_style() { @@ -78,9 +81,9 @@ ozi_set_style() /* set up mkshort */ if (global_opts.synthesize_shortnames) { - setshort_length(32); - setshort_whitespace_ok(0); - setshort_badchars(xcsv_file.badchars); + setshort_length(mkshort_handle, 32); + setshort_whitespace_ok(mkshort_handle, 0); + setshort_badchars(mkshort_handle, xcsv_file.badchars); } } @@ -101,6 +104,8 @@ ozi_wr_init(const char *fname, const char *args) { ozi_set_style(); + mkshort_handle = mkshort_new_handle(); + xcsv_file.xcsvfp = fopen(fname, "w"); if (xcsv_file.xcsvfp == NULL) { diff --git a/gpsbabel/pcx.c b/gpsbabel/pcx.c index cb81973e4..e404ad6e7 100644 --- a/gpsbabel/pcx.c +++ b/gpsbabel/pcx.c @@ -24,6 +24,7 @@ static FILE *file_in; static FILE *file_out; +static void *mkshort_handle; #define MYNAME "PCX" @@ -47,6 +48,8 @@ static void wr_init(const char *fname, const char *args) { file_out = fopen(fname, "w"); + mkshort_handle = mkshort_new_handle(); + if (file_out == NULL) { fatal(MYNAME ": Cannot open %s for writing\n", fname); } @@ -56,6 +59,7 @@ static void wr_deinit(void) { fclose(file_out); + mkshort_del_handle(mkshort_handle); } static void @@ -118,7 +122,8 @@ gpsutil_disp(const waypoint *wpt) fprintf(file_out, "W %-6.6s %c%08.5f %c%011.5f %s %5d %-40.40s %5e %s\n", global_opts.synthesize_shortnames ? - mkshort(wpt->description) : wpt->shortname, + mkshort(mkshort_handle, wpt->description) : + wpt->shortname, lat < 0.0 ? 'S' : 'N', fabs(lat), lon < 0.0 ? 'W' : 'E', @@ -144,7 +149,7 @@ fprintf(file_out, "U LAT LON DM\n" "\n" "H IDNT LATITUDE LONGITUDE DATE TIME ALT DESCRIPTION PROXIMITY SYMBOL ;waypts\n"); - setshort_length(6); + setshort_length(mkshort_handle, 6); waypt_disp_all(gpsutil_disp); } diff --git a/gpsbabel/psp.c b/gpsbabel/psp.c index 3e8609266..e23793fb2 100644 --- a/gpsbabel/psp.c +++ b/gpsbabel/psp.c @@ -35,6 +35,7 @@ static FILE *psp_file_in; static FILE *psp_file_out; +static FILE *mkshort_handle; static int i_am_little_endian; static int endianness_tested; @@ -259,6 +260,8 @@ static void psp_wr_init(const char *fname, const char *args) { psp_file_out = fopen(fname, "wb"); + mkshort_handle = mkshort_new_handle(); + if (psp_file_out == NULL) { fatal(MYNAME ": Cannot open %s for writing\n", fname); } @@ -267,6 +270,7 @@ psp_wr_init(const char *fname, const char *args) static void psp_wr_deinit(void) { + mkshort_del_handle(mkshort_handle); fclose(psp_file_out); } @@ -404,7 +408,7 @@ psp_waypt_pr(const waypoint *wpt) if ((! wpt->shortname) || (global_opts.synthesize_shortnames)) { if (wpt->description) { if (global_opts.synthesize_shortnames) - shortname = mkshort(wpt->description); + shortname = mkshort(mkshort_handle, wpt->description); else shortname = xstrdup(wpt->description); } else { @@ -518,8 +522,8 @@ psp_write(void) s = waypt_count(); if (global_opts.synthesize_shortnames) { - setshort_length(32); - setshort_whitespace_ok(1); + setshort_length(mkshort_handle, 32); + setshort_whitespace_ok(mkshort_handle, 1); } if (s > MAXPSPOUTPUTPINS) { diff --git a/gpsbabel/tiger.c b/gpsbabel/tiger.c index e6b1ca90e..ecf9370a1 100644 --- a/gpsbabel/tiger.c +++ b/gpsbabel/tiger.c @@ -24,6 +24,7 @@ static FILE *file_in; static FILE *file_out; +static void *mkshort_handle; #define MYNAME "GPSUTIL" @@ -31,6 +32,8 @@ static void rd_init(const char *fname, const char *args) { file_in = fopen(fname, "r"); + mkshort_handle = mkshort_new_handle(); + if (file_in == NULL) { fatal(MYNAME ": Cannot open %s for reading\n", fname); } @@ -40,12 +43,14 @@ static void rd_deinit(void) { fclose(file_in); + mkshort_del_handle(mkshort_handle); } static void wr_init(const char *fname, const char *args) { file_out = fopen(fname, "w"); + if (file_out == NULL) { fatal(MYNAME ": Cannot open %s for writing\n", fname); } @@ -63,18 +68,21 @@ data_read(void) double lat,lon; char desc[100]; char icon[100]; + char ibuf[1024]; waypoint *wpt_tmp; - while( fscanf(file_in, "%lf,%lf:%100[^:]:%100[^\n]", - &lon, &lat, icon, desc) > 0) { - wpt_tmp = xcalloc(sizeof (*wpt_tmp), 1); + while (fgets(ibuf, sizeof(ibuf), file_in)) { + if( sscanf(ibuf, "%lf,%lf:%100[^:]:%100[^\n]", + &lon, &lat, icon, desc)) { + wpt_tmp = xcalloc(sizeof (*wpt_tmp), 1); - wpt_tmp->position.longitude.degrees = lon; - wpt_tmp->position.latitude.degrees = lat; - wpt_tmp->description = xstrdup(desc); - wpt_tmp->shortname = mkshort(desc); + wpt_tmp->position.longitude.degrees = lon; + wpt_tmp->position.latitude.degrees = lat; + wpt_tmp->description = xstrdup(desc); + wpt_tmp->shortname = mkshort(mkshort_handle, desc); - waypt_add(wpt_tmp); + waypt_add(wpt_tmp); + } } } diff --git a/gpsbabel/tmpro.c b/gpsbabel/tmpro.c index 1f9dea675..529d4f1ea 100644 --- a/gpsbabel/tmpro.c +++ b/gpsbabel/tmpro.c @@ -40,6 +40,7 @@ static FILE *file_in; static FILE *file_out; +static void *mkshort_handle; static void rd_init(const char *fname, const char *args) @@ -185,7 +186,7 @@ tmpro_waypt_pr(const waypoint * wpt) if ((! wpt->shortname) || (global_opts.synthesize_shortnames)) { if (wpt->description) { if (global_opts.synthesize_shortnames) - shortname = mkshort(wpt->description); + shortname = mkshort(mkshort_handle, wpt->description); else shortname = csv_stringclean(wpt->description, ",\""); } else { @@ -234,15 +235,17 @@ data_write(void) { /* Short names */ if (global_opts.synthesize_shortnames) { - setshort_length(6); - setshort_whitespace_ok(0); - setshort_badchars("\","); + mkshort_handle = mkshort_new_handle(); + setshort_length(mkshort_handle, 6); + setshort_whitespace_ok(mkshort_handle, 0); + setshort_badchars(mkshort_handle, "\","); } /* Write file header */ fprintf(file_out, "Group\tsID\tsDescription\tfLat\tfLong\tfEasting\tfNorthing\tfAlt\tiColour\tiSymbol\tsHyperLink\n"); waypt_disp_all(tmpro_waypt_pr); + mkshort_del_handle(mkshort_handle); } ff_vecs_t tmpro_vecs = { diff --git a/gpsbabel/tpg.c b/gpsbabel/tpg.c index ad756ea8e..8ef169dda 100644 --- a/gpsbabel/tpg.c +++ b/gpsbabel/tpg.c @@ -30,6 +30,7 @@ static FILE *tpg_file_in; static FILE *tpg_file_out; +static void *mkshort_handle; static int i_am_little_endian; static int endianness_tested; @@ -146,6 +147,8 @@ static void tpg_wr_init(const char *fname, const char *args) { tpg_file_out = fopen(fname, "wb"); + mkshort_handle = mkshort_new_handle(); + if (tpg_file_out == NULL) { fatal(MYNAME ": Cannot open %s for writing\n", fname); } @@ -266,7 +269,7 @@ tpg_waypt_pr(const waypoint *wpt) if ((! wpt->shortname) || (global_opts.synthesize_shortnames)) { if (wpt->description) { if (global_opts.synthesize_shortnames) - shortname = mkshort(wpt->description); + shortname = mkshort(mkshort_handle, wpt->description); else shortname = xstrdup(wpt->description); } else { @@ -351,8 +354,8 @@ tpg_write(void) s = waypt_count(); if (global_opts.synthesize_shortnames) { - setshort_length(32); - setshort_whitespace_ok(1); + setshort_length(mkshort_handle, 32); + setshort_whitespace_ok(mkshort_handle, 1); } if (s > MAXTPGOUTPUTPINS) { diff --git a/gpsbabel/waypt.c b/gpsbabel/waypt.c index 761d17bd2..49704b4d5 100644 --- a/gpsbabel/waypt.c +++ b/gpsbabel/waypt.c @@ -24,10 +24,12 @@ queue waypt_head; static unsigned int waypt_ct; +static void *mkshort_handle; void waypt_init(void) { + mkshort_handle = mkshort_new_handle(); QUEUE_INIT(&waypt_head); } @@ -66,8 +68,9 @@ waypt_disp(const waypoint *wpt) printposn(&wpt->position.longitude,0); printf("%s/%s", global_opts.synthesize_shortnames ? - mkshort(wpt->description) : wpt->shortname, - wpt->description); + mkshort(mkshort_handle, wpt->description) : + wpt->shortname, + wpt->description); if (wpt->position.altitude.altitude_meters != unknown_alt) printf(" %f", wpt->position.altitude.altitude_meters); printf("\n"); diff --git a/gpsbabel/xcsv.c b/gpsbabel/xcsv.c index b03bb14dd..74b08a25e 100644 --- a/gpsbabel/xcsv.c +++ b/gpsbabel/xcsv.c @@ -30,6 +30,8 @@ #define MYNAME "XCSV" #define ISSTOKEN(a,b) (strncmp(a,b, strlen(b)) == 0) +static void *mkshort_handle; + /* a table of config file constants mapped to chars */ static char_map_t xcsv_char_table[] = { @@ -329,6 +331,7 @@ static void xcsv_wr_init(const char *fname, const char *args) { const char * p; + mkshort_handle = mkshort_new_handle(); /* if we don't have an internal style defined, we need to * read it from a user-supplied style file, or die trying. @@ -345,17 +348,17 @@ xcsv_wr_init(const char *fname, const char *args) if (global_opts.synthesize_shortnames) { p = get_option(args, "snlen"); if (p) - setshort_length(atoi(p)); + setshort_length(mkshort_handle, atoi(p)); p = get_option(args, "snwhite"); if (p) - setshort_whitespace_ok(atoi(p)); + setshort_whitespace_ok(mkshort_handle, atoi(p)); p = get_option(args, "snupper"); if (p) - setshort_mustupper(atoi(p)); + setshort_mustupper(mkshort_handle, atoi(p)); - setshort_badchars(xcsv_file.badchars); + setshort_badchars(mkshort_handle, xcsv_file.badchars); } } diff --git a/gpsbabel/xmapwpt.c b/gpsbabel/xmapwpt.c index c1e834f79..fa3da7873 100644 --- a/gpsbabel/xmapwpt.c +++ b/gpsbabel/xmapwpt.c @@ -27,6 +27,7 @@ #include "csv_util.h" #define MYNAME "XMAPWPT" +static void *mkshort_handle; static void xmapwpt_set_style() @@ -66,9 +67,9 @@ xmapwpt_set_style() /* set up mkshort */ if (global_opts.synthesize_shortnames) { - setshort_length(32); - setshort_whitespace_ok(0); - setshort_badchars(xcsv_file.badchars); + setshort_length(mkshort_handle, 32); + setshort_whitespace_ok(mkshort_handle, 0); + setshort_badchars(mkshort_handle, xcsv_file.badchars); } } @@ -87,6 +88,8 @@ xmapwpt_rd_init(const char *fname, const char *args) static void xmapwpt_wr_init(const char *fname, const char *args) { + mkshort_handle = mkshort_new_handle(); + xmapwpt_set_style(); xcsv_file.xcsvfp = fopen(fname, "w"); -- 2.30.2